package com.gdpu.project.util.xml;

import java.io.File;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * 格式化XML文档,并解决中文问题
 */

public class FormatXML {
	public static void formatXml(String fileName) {
		try {
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new File(fileName));
			XMLWriter output = null;
			/** 格式化输出,类型IE浏览一样 */
			OutputFormat format = OutputFormat.createPrettyPrint();
			/** 指定XML字符集编码 */
			format.setEncoding("utf-8");
			output = new XMLWriter(new FileWriter(new File(fileName)), format);
			output.write(document);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("格式化出错啦!");
		}
	}

}
